﻿using B.Z.BasicData.Infrastructure;
using B.Z.Inventory.API.Read.Application.Command;
using B.Z.Inventory.API.Read.DTO;
using B.Z.Inventory.Domain;
using B.Z.Inventory.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Net.Mime;

namespace B.Z.Inventory.API.Read.Controllers
{
    /// <summary>
    /// 库存盘点控制器（读）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryReadController : ControllerBase
    {


        private readonly IMediator mediator;
        private readonly IBaseRepository<T_Goods> goodsRepository;
        public InventoryReadController(IMediator mediator, IBaseRepository<T_Goods> goodsRepository)
        {
            this.mediator = mediator;
            this.goodsRepository = goodsRepository;
        }
        /// <summary>
        /// 盘点任务列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<APIPaging<InventoryDto>>> InventoryTaskList([FromQuery]InventoryDtoQueryCommand request) 
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>仓库列表</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<StoreDto>>> GetStoreList()
        {
            StoreQueryCommand request = new StoreQueryCommand();
            return mediator.Send(request);
        }

        /// <summary>
        /// 根据ID获取库存盘点任务详情
        /// </summary>
        /// <param name="id">盘点任务ID</param>
        /// <returns>盘点任务详情</returns>
        [HttpGet]
        public Task<APIResult<DataInventoryDto>> GetInventoryTaskById(int id)
        {
            InventoryDataCommand request = new InventoryDataCommand { Id = id };
            return mediator.Send(request);
        }

        /// <summary>
        /// 获取货品列表（支持搜索）
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>货品列表</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<GoodsDto>>> GetGoodsList([FromQuery] GoodsQueryCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 获取出入库单列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>出入库单列表</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<IntOutStoreDto>>> GetIntOutStoreList([FromQuery] IntOutStoreQueryCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 导出Excel(货品)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpDownGoodsExcel(string ids)
        {

            var idstring= ids.Split(",").Select(x => Convert.ToInt32(x));
            var list = goodsRepository.GetAll().Where(x => idstring.Contains(x.Id));

            XSSFWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("盘点货物报表");
            IRow row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("货品名称");
            row.CreateCell(1).SetCellValue("货品编号");
            row.CreateCell(2).SetCellValue("货品规格");
            row.CreateCell(3).SetCellValue("货品单位");
            row.CreateCell(4).SetCellValue("系统库存数量");
            row.CreateCell(5).SetCellValue("实际盘点数量");
            row.CreateCell(6).SetCellValue("差异数量");
            row.CreateCell(7).SetCellValue("货品单价");
            row.CreateCell(8).SetCellValue("备注");

         
            int num = 1;
            foreach (var item in list)
            {
                IRow rows = sheet.CreateRow(num);
                rows.CreateCell(0).SetCellValue(item.GoodsName);
                rows.CreateCell(1).SetCellValue(item.GoodsNumber);
                rows.CreateCell(2).SetCellValue(item.GoodsModel);
                rows.CreateCell(3).SetCellValue(item.GoodsUnit);
                rows.CreateCell(4).SetCellValue((int)item.SystemNum);
                rows.CreateCell(5).SetCellValue((int)item.ActualNum);
                rows.CreateCell(6).SetCellValue((item.SystemNum>item.ActualNum ? item.SystemNum - item.ActualNum : item.ActualNum - item.SystemNum).ToString());
                rows.CreateCell(7).SetCellValue(item.OnePrice.ToString());
                rows.CreateCell(8).SetCellValue("");
                num++;
            }
            byte[] bt = null;
            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bt = ms.ToArray();
            };

            return File(bt, MediaTypeNames.Application.Octet, "盘点货物报表.xlsx");


        }
    }
}
